using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace com.haopintui.util
{
	public class AlimamaLogin
	{
		public static void login(CmsForm cmsForm)
		{
			bool alimama_auto_login = cmsForm.appBean.alimama_auto_login && !cmsForm.appBean.alimama_acc.Equals("") && !cmsForm.appBean.alimama_pwd.Equals("");
			AlimamaLogin.login(cmsForm, alimama_auto_login);
		}
		public static void login(CmsForm cmsForm, bool alimama_auto_login)
		{
			AlimamaLogin.login(cmsForm, alimama_auto_login, cmsForm.appBean.alimama_acc, cmsForm.appBean.alimama_pwd);
		}
		public static void login(CmsForm cmsForm, bool alimama_auto_login, string alimama_acc, string alimama_pwd)
		{
			try
			{
				if (Process.GetProcessesByName("AlimamaLogin").Length > 0)
				{
					LogUtil.log_str(cmsForm, "登录窗口已经打开。。。。请稍候！");
				}
				else
				{
					FormUtil.set_formWindowState(cmsForm, FormWindowState.Minimized);
					LogUtil.log_str(cmsForm, "正在打开登录窗口。。。。");
					string text = alimama_auto_login ? "1" : "0";
					string text2 = " 0";
					if (!cmsForm.appBean.uu_username.Equals("") && !cmsForm.appBean.uu_pwd.Equals(""))
					{
						text2 = " 1 " + cmsForm.appBean.uu_username + " " + cmsForm.appBean.uu_pwd;
					}
					string arguments = string.Concat(new string[]
					{
						"\"",
						cmsForm.Text,
						"\" ",
						text,
						" ",
						alimama_acc,
						" ",
						alimama_pwd,
						" ",
						text2
					});
					try
					{
						Process.Start(cmsForm.app_path + "\\AlimamaLogin.exe", arguments);
						cmsForm.appBean.alimama_login_status = false;
					}
					catch
					{
						if (File.Exists(cmsForm.app_path + "\\AlimamaLogin.exe"))
						{
							File.Delete(cmsForm.app_path + "\\AlimamaLogin.exe");
						}
						try
						{
							cmsForm.webClient.DownloadFile(Constants.software_url + "/update/AlimamaLogin.exe", cmsForm.app_path + "\\AlimamaLogin.exe");
						}
						catch
						{
							cmsForm.webClient.DownloadFile(Constants.software_url + "/update/AlimamaLogin.exe", cmsForm.app_path + "\\AlimamaLogin.exe");
						}
						Process.Start(cmsForm.app_path + "\\AlimamaLogin.exe", arguments);
						cmsForm.appBean.alimama_login_status = false;
					}
				}
			}
			catch (Exception ex)
			{
				LogUtil.log_str(cmsForm, "[openLoginForm]出错，" + ex.ToString());
			}
		}
		public static void log_login(CmsForm cmsForm, int login_status_int, string cookie)
		{
			try
			{
				cmsForm.appBean.alimama_login_status = true;
				FormUtil.set_formWindowState(cmsForm, FormWindowState.Normal);
				if (login_status_int == Constants.FORM_MSG_TYPE_LOGINED)
				{
					cmsForm.appBean.alimama_login_status = true;
					cmsForm.appBean.taoke_cookie = cookie;
					LogUtil.log_str(cmsForm, "阿里妈妈登录完成，返回状态：【" + (AlimamaUtil.check_login(cookie) ? "登录成功" : "登录失败") + "】！");
					new Thread(new ParameterizedThreadStart(AlimamaAdUtil.updAliPid)).Start(cmsForm);
				}
				else
				{
					if (login_status_int == Constants.FORM_MSG_TYPE_NOTOPEN)
					{
						cmsForm.appBean.alimama_login_status = false;
						LogUtil.log_str(cmsForm, "网页无法打开！");
						if (cmsForm.appBean.alimama_auto_login)
						{
							AlimamaLogin.login(cmsForm);
						}
					}
					else
					{
						if (login_status_int == Constants.FORM_MSG_TYPE_CHKTMOUT)
						{
							cmsForm.appBean.alimama_login_status = false;
							LogUtil.log_str(cmsForm, "检查登录成功页面超过8秒！");
							if (cmsForm.appBean.alimama_auto_login)
							{
								AlimamaLogin.login(cmsForm);
							}
						}
						else
						{
							if (login_status_int == Constants.FORM_MSG_TYPE_CLOSENOTLOGINED)
							{
								cmsForm.appBean.alimama_login_status = false;
								LogUtil.log_str(cmsForm, "登录窗口被手动关闭，并且没有登录成功！");
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				LogUtil.log_str(cmsForm, "[processFormMsg]出错，" + ex.ToString());
			}
		}
	}
}
